package com.hnkywl.sts.service.base;

import com.hnkywl.sts.dao.base.DxrzDao;
import com.hnkywl.sts.entity.base.Dxrz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-20
 * Time: 下午8:49
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DxrzService {
    private DxrzDao dxrzDao;

    /**
     * 短信日志的列表查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findDxrzList(Date fsrq, int pageNo, int pageSize) {
        Page<Dxrz> DxrzPage = this.dxrzDao.findDxrzList(fsrq, pageNo, pageSize);
        List<Dxrz> DxrzList = DxrzPage.getResult() == null ? new ArrayList<Dxrz>() : DxrzPage.getResult();
        for (int i = 0; i < DxrzList.size(); i++) {
            DxrzList.get(i).setContent(DxrzList.get(i).getContent().replaceAll("\r\n", "<br/>"));
        }
        Map<String, Object> otherParam = new HashMap<String, Object>();
        String DxrzJson = BaseUtils.fetchCollectionToJson(DxrzList, otherParam, "id", "fsrq", "fsrXms", "fshms", "content","sffscg","errormessage");
        return "{\"total\":" + DxrzPage.getTotalCount() + ",\"rows\":[" + DxrzJson + "]}";
    }

    /**
     * 新增短信日志
     *
     * @param YgList
     * @param content
     * @throws RuntimeException
     */
    @Transactional
    public void saveDxrz(List<Yg> YgList, String content, Boolean flag, String message) {
        Dxrz dxrz = new Dxrz();
        dxrz.setFsrq(new Date());
        dxrz.setFsrXms(getXmFromYgList(YgList));
        dxrz.setFshms(getdhhmFromYgList(YgList));
        dxrz.setSffscg(flag);
        dxrz.setErrormessage(message);
        dxrz.setContent(content);
        dxrz.setDateCreated(new Date());
        dxrz.setLastUpdated(new Date());
        dxrz.setCreateUser(YgService.getCurrentYg());
        dxrz.setCreateOrgan(YgService.getCurrentYg().getZggs());
        dxrz.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.dxrzDao.save(dxrz);
    }

    /**
     * 新增短信日志
     *
     * @param content
     * @throws RuntimeException
     */
    @Transactional
    public void saveDxrz(String phone, String content, Boolean flag, String message) {
        Dxrz dxrz = new Dxrz();
        dxrz.setFsrq(new Date());
        dxrz.setFshms(phone);
        dxrz.setFsrXms(phone);
        dxrz.setSffscg(flag);
        dxrz.setErrormessage(message);
        dxrz.setContent(content);
        dxrz.setDateCreated(new Date());
        dxrz.setLastUpdated(new Date());
//        dxrz.setCreateUser(YgService.getCurrentYg());
//        dxrz.setCreateOrgan(YgService.getCurrentYg().getZggs());
//        dxrz.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.dxrzDao.save(dxrz);
    }

    private String getXmFromYgList(List<Yg> ygList) {
        String xms = "";
        for (Yg yg : ygList) {
            if (yg.getXm() != null) {
                xms += yg.getXm();
            } else {
                xms += yg.getDhhm();
            }
        }
        return xms.toString();
    }

    private String getdhhmFromYgList(List<Yg> ygList) {
        String dhhm = "";
        for (Yg yg : ygList) {
            dhhm += yg.getDhhm() == null ? "" : yg.getDhhm();
        }
        return dhhm.toString();
    }

    /**
     * 查询短信日志实例
     *
     * @param id
     * @return
     */
    @Transactional
    public Dxrz findDxrzDetail(Long id) {
        return this.dxrzDao.get(id);
    }

    @Autowired
    public void setDxrzDao(DxrzDao dxrzDao) {
        this.dxrzDao = dxrzDao;
    }
}
